﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using ZLCMS.Web.SystemManage.Control;
using System.Collections.Generic;
using NHibernate;
using DotNet.Common.TransferParam;
using NHibernate.Criterion;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.SystemManage.Basics.Advert
{
    public partial class AdvertEdit : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        ShopAdvertInfo advertDB ;
        protected void Page_Load(object sender, EventArgs e)
        {


            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();
            //初始化DWZ控件

            _framePage = new FrameHelper(this);
            if (IsPostBack)
            {
                SaveBussines();
            }
        }

        private void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                ParamKit.Bind("ShopAdvertPosition", ddl_position, null, null, null, "-1", null);
                ParamKit.Bind("YesNo_10", ddl_status, null, null, null, "0", null);

                txt_title.Text = advertDB.Title;
                txt_des.Text = advertDB.Description;
                ddl_position.SelectedValue = advertDB.PositionCode;
                txtUrl.Text = advertDB.Url;
                fc_relatedimgs.Value = advertDB.PhotoUrl;
                txt_sort.Text = advertDB.Sort.ToString();
                txt_param1.Text = advertDB.Param1;
                //txt_param2.Text = advertDB.Param2;
                //txt_param3.Text=advertDB.Param3;
                //txt_param4.Text=advertDB.Param4 ;
                //txt_param5.Text = advertDB.Param5;
                ddl_status.SelectedValue = advertDB.Status;
            }
        }

        private void InitializeGlobalValue()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (!IsPostBack)
                    {
                        _id.Text = Request["uid"];
                    }

                    advertDB = session.Get<ShopAdvertInfo>(int.Parse(_id.Text));

                    if (advertDB == null)
                    {
                        throw new Exception("找不到对应信息.");
                    }
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
        }

        #region  +protected void AddBussines()
        /// <summary>
        /// 
        /// </summary>
        protected void SaveBussines()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                advertDB.Title = txt_title.Text.Trim();
                advertDB.Description = txt_des.Text;
                advertDB.PositionCode = ddl_position.SelectedValue;
                advertDB.Url = txtUrl.Text;
                advertDB.PhotoUrl = fc_relatedimgs.Value;
                advertDB.Sort = Convert.ToInt32(string.IsNullOrEmpty(txt_sort.Text.Trim()) ? "9999" : txt_sort.Text.Trim());
                advertDB.Param1 = txt_param1.Text.Trim();
                //advertDB.Param2 = txt_param2.Text.Trim();
                //advertDB.Param3 = txt_param3.Text.Trim();
                //advertDB.Param4 = txt_param4.Text.Trim();
                //advertDB.Param5 = txt_param5.Text.Trim();
                advertDB.Status = ddl_status.SelectedValue;

                session.Update(advertDB);
                session.Flush();

                _framePage.Alert(new ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功!",
                    callbackType = "closeCurrent",
                    navTabId = _framePage.NavTabId
                });
            }
        }
        #endregion

    }
}
